<template>
  <div class="login">
    <div class="login-container">
      <h1>登录</h1> 
      <div class="connect">
        <!-- <p>超能小食堂进销存管理系统</p> -->
      </div>
      <div>
        <el-input v-model="userName"
                  placeholder="用户名"></el-input>
        
      </div>  
      <div class="int-box">
        <el-input v-model="password" type="password"
                  placeholder="输入密码"></el-input>
      </div> 
    </div>
    <div class="submit-box">
        <el-button @click="submit"
                width="100%"
                type="primary">登 录</el-button> 
    </div>
    
  </div> 
</template>

<script>
import { Message } from 'element-ui'
import api from '@/api/api.js'
import { JSEncrypt } from 'jsencrypt'
export default {
  name: 'login',
  data () {
    return {
      passwordKey: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYqxNMjTRQIvf9JylgBaCOwdAvE3XwWJownq8K8iy5FDMaC7wYHmbw6s+B66TcOYLZFy10mUYxkV+J+epmpnIxMSdXYZ7sU2pqbB9tZmtdkiy4YLoXQUirwaZsEJSApQF/P9StfqAralyG2wguBYyujF8EdV4Kbrb5vD+QTxcwxQIDAQAB',
      userName:'',
      password:'',
    }
  },
  mounted () { },
  methods: {
    submit(){
      if(!this.userName){
        return this.$vux.toast.show({
          text: '请输入用户名',
          type:'text'
        })
      }
      if(!this.password){
        return this.$vux.toast.show({
          text: '请输入密码',
          type:'text'
        })
      }
      let params = {
        userName: this.userName, 
      }
      let encrypt = new JSEncrypt(); 
      encrypt.setPublicKey(this.passwordKey);
      let password_Encry = encrypt.encrypt(this.password);
      params.password = password_Encry; 
      api.user.login(params,res=>{
        console.log(res);
        //登录之后信息存到缓存
        localStorage.cp_userInfo = JSON.stringify(res.result);
        sessionStorage.cp_userInfo = JSON.stringify(res.result); 
        // if(!res.result.openid){ 
        //   const last_url =   "/entirety";
        //   let url = window.location.protocol +"//"+ window.location.host  + last_url; 
        //   location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx90569d755e9f2b96&redirect_uri=${url}&response_type=code&scope=snsapi_base#wechat_redirect`
        //   return
         
        // }
        // sessionStorage.cp_openid = res.result.openid;
        // if(sessionStorage.storeUrl){
        //   location.href = sessionStorage.storeUrl;
        //   return
        // } 
        this.$router.push("/entirety")
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped> 
.login{
    padding: 80px 20px 0;
    text-align: center;
}
.submit-box{
    margin-top: 40px;
    
    /deep/button{
        width: 100%;
    }
}
.login-container{
   .connect{
       padding: 40px 0;
   } 
}
.int-box{
    margin-top: 20px;
}
</style>
